/*
Use an Arduino to drive relays from EMC2 HAL
In this example, I'm using a 4-relays card with inverted input. If you drive directly the relays, redefine MYHIGH to HIGH and MYLOW to LOW.
*/
 
#define MYHIGH LOW
#define MYLOW  HIGH
int relay_pin[] = {3, 4, 5, 6};
 
char in[2];
 
void setup () {
  int i = 0;
  while (relay_pin[i]) {
    digitalWrite (relay_pin[i], MYLOW);
    pinMode (relay_pin[i], OUTPUT);
    i++;
  }
  Serial.begin (115200);
}
 
void loop () {
  static int in_pos = 0;
  char c;
  int i;
  while (Serial.available ()) {
    char c = (char) Serial.read ();
    if (c == '\r' || c == '\n') {
      if (in_pos >= 2) {
        i = in[0] - 1;
        if (i >= 0 && i <= 3) {
          digitalWrite (relay_pin[i], (in[1] > 0) ? MYHIGH : MYLOW);
        }
      }
      in_pos = 0;
    } else {
      in[in_pos++] = c - 48;
    }
  }
}
